package com.wntms.wgj.web;

import com.github.pagehelper.PageInfo;
import com.wntms.gps.pojo.Result;
import com.wntms.wgj.pojo.CollectionSettle;
import com.wntms.wgj.pojo.DriverSettle;
import com.wntms.wgj.service.CollectionSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/collectionsettle")
public class CollectionSettleController {
    @Autowired
    private CollectionSettleService collectionContractService;

    /**
     * 查询所有代收货款信息
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<CollectionSettle> list = collectionContractService.findAll();
        Result<Object> result = new Result<>();
        if (list == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else {
            result.setCode(200);
            result.setMsg("结算成功");
            result.setData(list);
        }

        return result;
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<CollectionSettle> list = collectionContractService.findAll(currentPage,pageSize);

        Result<Object> result = new Result<>();
        if (list != null){
            PageInfo<CollectionSettle> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    /**
     * 代收结算
     * @param collectionSettle
     * @return
     */
    @PutMapping
    public Result updateSettle(@RequestBody CollectionSettle collectionSettle){
        boolean flag = collectionContractService.updateSettle(collectionSettle);
        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("修改失败");
        }else {
            result.setCode(200);
            result.setMsg("修改成功");
        }

        return result;
    }

    /**
     * 删除代收结算记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteSettle(@PathVariable Integer id){
        boolean flag = collectionContractService.deleteSettle(id);

        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("删除失败");
        }else {
            result.setCode(200);
            result.setMsg("删除成功");
        }

        return result;
    }

    @GetMapping("/{fid}")
    public CollectionSettle findByFid(@PathVariable String fid){
        CollectionSettle collectionSettle = collectionContractService.findByFid(fid);
        Result<Object> result = new Result<>();

        if (collectionSettle != null){
            result.setCode(200);
            result.setMsg("查询单条成功");
            result.setData(collectionSettle);
        }else {
            result.setCode(200);
            result.setMsg("查询单条失败");
        }

        return result;
    }
}
